package cn.smartaccount.demo.service;

import cn.smartaccount.demo.common.util.CollectionUtils;
import cn.smartaccount.demo.dto.Response;
import cn.smartaccount.demo.infrastructure.entity.UserDO;
import cn.smartaccount.demo.infrastructure.mapper.UserDOMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuluquan
 * @date 2023-02-14 22:00
 */
@Service
public class AccountService {

    private static final Logger logger = LoggerFactory.getLogger(AccountService.class);

    @Autowired
    private UserDOMapper userDOMapper;

    public Response getUserInfo(String padSerial) {

        logger.info("获取用户信息={}", padSerial);

        if (StringUtils.isBlank(padSerial)) {
            return Response.error(2001, "pad序列号为空");
        }


        Example userExample = new Example(UserDO.class);
        Example.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andEqualTo("padClientId", padSerial);

        List<UserDO> userDOS = userDOMapper.selectByExample(userExample);

        Map<String, Object> userInfo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(userDOS)) {
            Map<String, Object> detail = new HashMap<>();
            detail.put("userId", userDOS.get(0).getId());
            userInfo.put("userInfo", detail);
        }

        return Response.success(userInfo);

    }
}
